package com.test.booksystem.service;

import com.test.booksystem.mapper.BookMapper;
import com.test.booksystem.model.BookInfo;
import com.test.booksystem.model.BookStatus;
import com.test.booksystem.model.PageRequest;
import com.test.booksystem.model.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BookService {

    @Autowired
    private BookMapper bookMapper;

    public Integer addBook(BookInfo bookInfo) {
        return bookMapper.addBook(bookInfo);
    }

    public PageResult<BookInfo> getBookInfoByPage(PageRequest pageRequest) {
        // 1.获取数据总数量
        Integer total = bookMapper.countTotalBookInfo();
        // 2.获取当前页码对应的数据
        List<BookInfo> bookInfos = bookMapper.queryBookInfoByPage(pageRequest.getOffset(),pageRequest.getPage());
        // 3.处理图书状态
        // 0 - 无效; 1 - 可借阅;  2 - 不可借阅
        for (BookInfo bookInfo : bookInfos) {
            bookInfo.setStatusCN(BookStatus.getStatusByCode(bookInfo.getStatus()).getStatusCN());
        }
        return new PageResult<BookInfo>(bookInfos,total,pageRequest);
    }

    public BookInfo queryBookById(Integer id) {
        BookInfo bookInfo = bookMapper.queryBookById(id);
        // 对 statusCN 进行处理(虽然也用不到这个，只是为了好看)
        bookInfo.setStatusCN(BookStatus.getStatusByCode(bookInfo.getStatus()).getStatusCN());
        return bookInfo;
    }

    public Integer updateBook(BookInfo bookInfo) {
        return bookMapper.updateBook(bookInfo);
    }

    public Integer deleteBook(Integer bookId) {
        BookInfo bookInfo = new BookInfo();
        bookInfo.setId(bookId);
        bookInfo.setStatus(0);
        return bookMapper.updateBook(bookInfo);
    }

    public Integer batchDeleteByIds(List<Integer> ids) {
        return bookMapper.batchDeleteBookByIds(ids);
    }
}
